<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd"><?php
session_start();
if (empty($_SESSION["loginusername"])) {
    Header("Location: firstphp.php");
}

if (!empty($_SESSION["userType"])) {
    $userType = $_SESSION["userType"];
}
?>
<html>
    <head>

        <link href="ie.css" rel="stylesheet" type="text/css" />

        <title>Existing Clients</title>
        <link rel="stylesheet" type="text/css" href="http://localhost/Eldercare/jquery/easyui.css">
        <link rel="stylesheet" type="text/css" href="http://localhost/Eldercare/jquery/icon.css">
        <script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery-1.4.4.min.js"></script>

        <script type="text/javascript" src="http://localhost/Eldercare/jquery/jquery.easyui.min.js"></script>
        <style type="text/css">
            #fm{
                margin:0;
                padding:10px 30px;
            }
            .ftitle{
                font-size:14px;
                font-weight:bold;
                color:#666;
                padding:5px 0;
                margin-bottom:10px;
                border-bottom:1px solid #ccc;
            }
            .fitem{
                margin-bottom:5px;
            }
            .fitem label{
                display:inline-block;
                width:80px;
            }
            .button 
            {
                background-color:beige;
                cursor:pointer; }
            </style>
            <link href="http://localhost/Eldercare/finalstyle.css" rel="stylesheet" type="text/css" media="all" />
            <script type="text/javascript">
                function myformatter(date) {
                    var y = date.getFullYear();
                    var m = date.getMonth() + 1;
                    var d = date.getDate();
                    return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
                }
                function myparser(s) {
                    if (!s)
                        return new Date();
                    var ss = (s.split('-'));
                    var y = parseInt(ss[0], 10);
                    var m = parseInt(ss[1], 10);
                    var d = parseInt(ss[2], 10);
                    if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
                        return new Date(y, m - 1, d);
                    } else {
                        return new Date();
                    }
                }


                var url;
                function newClient() {
                    $('#updatedlg').dialog('close');
                    $('#insertdlg').dialog('open').dialog('setTitle', 'New Client');
                    $('#insertform').form('clear');
                    url = 'CRUDClient/save_client.php';
                }
                function editClient($NRIC, $CustomerName, $Citizenship, $BeforeDeviation, $AfterDeviation, $DateOfBirth, $Gender, $CentreName) {
                    $('#insertdlg').dialog('close');
                    var row = $('#dg').datagrid('getSelected');
                    if (row) {
                        $('#updatedlg').dialog('open').dialog('setTitle', 'Edit Client');
                        $('#updateform').form('load', row);
                        url = 'CRUDClient/update_client.php?NRIC=' + $NRIC + ',CustomerName=' + $CustomerName + ',Citizenship=' + $Citizenship + ',BeforeDeviation=' + $BeforeDeviation + ',AfterDeviation=' + $AfterDeviation + ',DateOfBirth=' + $DateOfBirth + ',Gender=' + $Gender + ',CentreName=' + $CentreName;
                    }
                }

                function updateClient($NRIC, $CustomerName, $Citizenship, $BeforeDeviation, $AfterDeviation, $DateOfBirth, $Gender, $CentreName) {
                    $('#updateform').form('submit', {
                        url: 'CRUDClient/update_client.php?NRIC=' + $NRIC + ',CustomerName=' + $CustomerName + ',Citizenship=' + $Citizenship + ',BeforeDeviation=' + $BeforeDeviation + ',AfterDeviation=' + $AfterDeviation + ',DateOfBirth=' + $DateOfBirth + ',Gender=' + $Gender + ',CentreName=' + $CentreName,
                        onSubmit: function() {
                            return $(this).form('validate');
                        },
                        success: function(result) {
                            var result = eval('(' + result + ')');
                            if (result.success) {
                                $('#updatedlg').dialog('close');		// close the dialog
                                $('#dg').datagrid('reload');	// reload the user data
                            } else {
                                $.messager.show({
                                    title: 'Error',
                                    msg: result.msg
                                });
                            }
                        }
                    });
                }

                function saveClient($NRIC, $CustomerName, $Citizenship, $BeforeDeviation, $AfterDeviation, $DateOfBirth, $Gender, $CentreName) {
                    $('#insertform').form('submit', {
                        url: 'CRUDClient/save_client.php?NRIC=' + $NRIC + ',CustomerName=' + $CustomerName + ',Citizenship=' + $Citizenship + ',BeforeDeviation=' + $BeforeDeviation + ',AfterDeviation=' + $AfterDeviation + ',DateOfBirth=' + $DateOfBirth + ',Gender=' + $Gender + ',CentreName=' + $CentreName,
                        onSubmit: function() {
                            return $(this).form('validate');
                        },
                        success: function(result) {
                            var result = eval('(' + result + ')');
                            if (result.success) {
                                $('#insertdlg').dialog('close');		// close the dialog
                                $('#dg').datagrid('reload');	// reload the user data
                            } else {
                                $.messager.show({
                                    title: 'Error',
                                    msg: result.msg
                                });
                            }
                        }
                    });
                }

                function removeClient() {
                    var row = $('#dg').datagrid('getSelected');
                    if (row) {
                        $.messager.confirm('Confirm Delete Client', 'Are you sure you want to remove this client?', function(r) {
                            if (r) {
                                $.post('CRUDClient/remove_client.php', {NRIC: row.NRIC}, function(result) {
                                    if (result.success) {
                                        $('#dg').datagrid('reload');	// reload the user data
                                    } else {
                                        $.messager.show({// show error message
                                            title: 'Error',
                                            msg: result.msg
                                        });
                                    }
                                }, 'json');
                            }
                        });
                    }
                }
                function getClient() {
                    $('#dg').datagrid('load', {
                        searchid: $('#searchid').val()
                    });

                }
                function findAll() {
                    $('#dg').datagrid('load', {
                    });
                }
            </script>	
        </head>
        <body>
            <ul>
                <div class="banner"></div>
            <div id="main">
                <div class="left-wrapper">
                    <div class="logo">
                    </div>
                    <div class="menu-wrapper">
                        <div id='cssmenu' >     
                            <ul>
                                <?php
                                if ($userType == "Therapy Assistant") {
                                    //if TA
                                    echo "<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li class='last'><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			 </ul>
			  </li>
			  <li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
		 <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
   <li class='last'><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
     
			 ";
                                } else if ($userType == "Centre Manager") {
                                    echo "
			<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			<li><a href='http://localhost/Eldercare/BackDate.php'><span>BackDate</span></a></li>
			<li class='last'><a href='http://localhost/Eldercare/VerifyAttendance.php'><span>Verify Attendance</span></a></li>
			</ul>
			<li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/User.php'><span>User</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
    <li class='has-sub'><a href='#'><span>Password</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ForgetPasswords.php'><span>Forget Password</span></a></li>
      </ul>
   </li>
			";
                                } else {
                                    echo "
			<li class='has-sub' class='active'><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/AttendanceHome.php'><span>Attendance Taking</span></a></li>
			 <li><a href='http://localhost/Eldercare/ViewAttendanceRecord.php'><span>View Attendance</span></a></li>
			<li><a href='http://localhost/Eldercare/BackDate.php'><span>BackDate</span></a></li>
			<li class='last'><a href='http://localhost/Eldercare/VerifyAttendance.php'><span>Verify Attendance</span></a></li>
			</ul>
			<li class='has-sub'><a href='#'><span>Profile</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Client.php'><span>Existing Client</span></a></li>
         <li><a href='http://localhost/Eldercare/WClient.php'><span>Withdrawn Client</span></a></li>
         <li><a href='http://localhost/Eldercare/User.php'><span>User</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/Centres.php'><span>Centres</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='http://localhost/Eldercare/ActRecords.php'><span>Activities</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/Activities.php'><span>Manage Current Activities</span></a></li>
         <li><a href='http://localhost/Eldercare/ActRecords.php'><span>Activity Recording</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ViewActRecord.php'><span>View Activity Records</span></a></li>
      </ul>
   </li>
   <li><a href='http://localhost/Eldercare/Holidays.php'><span>Holiday</span></a></li>
<li class='has-sub'><a href='#'><span>Reports</span></a>
   <ul>
   <li><a href='http://localhost/Eldercare/Reports.php'><span>Attendance Reports</span></a></li>
   <li class='last'><a href='http://localhost/Eldercare/ActReports.php'><span>Activity Reports</span></a></li>
   </ul>
   </li>
    <li class='has-sub'><a href='#'><span>Password</span></a>
      <ul>
         <li><a href='http://localhost/Eldercare/ManagePassword.php'><span>Change Passwords</span></a></li>
         <li class='last'><a href='http://localhost/Eldercare/ForgetPasswords.php'><span>Forget Password</span></a></li>
      </ul>
   </li>
			";
                                }
                                ?>
                            </ul>
                        </div>
                        <div class="clear"></div></div></div>
                <div class="content">
                    <div class="content2">
                        <b style="float:right; margin: 0px 0 0 0">  <?php
                            //session_start();
                            if (!empty($_SESSION["centreName"])) {
                                $Username = $_SESSION["loginusername"];
                                $centreName = $_SESSION["centreName"];
                                echo "Welcome, " . $Username . " ! <br>" . $centreName . " Branch";
                            } else {
                                $Username = $_SESSION["loginusername"];
                                echo "Welcome, " . $Username;
                            }
                            ?>

                            <?php
                            ?>
                            <?php
//session_start();
// set timeout period in seconds
                            $inactive = 7200;
// check to see if $_SESSION['timeout'] is set
                            if (isset($_SESSION["timeout"])) {
                                $session_life = time() - $_SESSION["timeout"];
                                if ($session_life > $inactive) {
                                    session_destroy();
                                    Header("Location: Timeout.php");
                                }
                            }
                            $_SESSION["timeout"] = time();
                            ?>

                            <input type="submit" class="button" value="Logout" onclick="parent.location = 'logout.php'"></b>
                        </p>



                        <div style="float:center">
                            <br>
                            <h3>Existing Clients</h3>
                            <br>

                            <table id="dg" title="Clients" class="easyui-datagrid" style="width:auto;height:auto; "
                                   url="CRUDClient/get_clients.php"
                                   toolbar="#toolbar" pagination="true"
                                   rownumbers="true" fitColumns="true" singleSelect="true">
                                <thead>
                                    <tr>
                                        <th field="NRIC" width="50">NRIC</th>
                                        <th field="CustomerName" width="50">Name</th>
                                        <th field="Citizenship" width="50">Citizenship</th>
                                        <th field="BeforeDeviation" width="50">Before Deviation</th>
                                        <th field="AfterDeviation" width="50">After Deviation</th>
                                        <th field="DateOfBirth" width="50">Date of Birth</th>
                                        <th field="Gender" width="50">Gender</th>
                                        <th field="CentreName" width="50">Centre Name</th>
                                    </tr>
                                </thead>
                            </table>
                            <div id="toolbar">
                                <?php
                                if ($userType == "Therapy Assistant") {

                                    echo"
		<a href='#' class='easyui-linkbutton' iconCls='icon-edit' plain='true' onclick='editClient()'>Edit Client</a>
		";
                                } else if ($userType == "Centre Manager") {

                                    echo"<a href='#' class='easyui-linkbutton' iconCls='icon-add' plain='true' onclick='newClient()'>New Client</a>
		<a href='#' class='easyui-linkbutton' iconCls='icon-edit' plain='true' onclick='editClient()'>Edit Client</a>
                <a href='#' class='easyui-linkbutton' iconCls='icon-remove' plain='true' onclick='removeClient()'>Remove Client</a>";
                                } else {
                                    echo"<a href='#' class='easyui-linkbutton' iconCls='icon-add' plain='true' onclick='newClient()'>New Client</a>
		<a href='#' class='easyui-linkbutton' iconCls='icon-edit' plain='true' onclick='editClient()'>Edit Client</a>
		<a href='#' class='easyui-linkbutton' iconCls='icon-remove' plain='true' onclick='removeClient()'>Remove Client</a>";
                                }
                                ?>

                                <input id="searchid" name="searchid" type="text" style="border-radius:25px">
                                <a href="#" class="easyui-linkbutton" iconCls="icon-search" onClick="getClient()">Search</a>
                                <a href="#" class="easyui-linkbutton" onClick="findAll()">All</a>
                            </div>


                            <!-- Insert Dialog and Form -->
                            <div>
                                <div id="insertdlg" class="easyui-dialog" style="width:400px;height:380px;padding:10px 20px"
                                     closed="true" buttons="#insertdlg-buttons">
                                    <div class="ftitle">Clients Information</div>
                                    <form id="insertform" method="post" novalidate>
                                        <div class="fitem">
                                            <label>NRIC: </label>
                                            <input name="NRIC" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Name: </label>
                                            <input name="CustomerName" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Citizenship: </label>
                                            <select name="Citizenship"required ="true" class="easyui-combobox" style="width:155" height='auto'>
                                                <option value="SC">Sinagapore Citizen</option>
                                                <option value="PR">Permanent Resident</option>
                                            </select>
                                        </div>
                                        <div class="fitem">
                                            <label>Before Deviation: </label>
                                            <input name="BeforeDeviation" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>After Deviation: </label>
                                            <input name="AfterDeviation" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Date of Birth: </label>
                                            <input name="DateOfBirth" type="date" value "" data-options="formatter:myformatter,parser:myparser" class="easyui-datebox" required="true"style="width:155" height='auto' >

                                        </div>
                                        <div class="fitem">
                                            <label>Gender: </label>
                                            <select name="Gender"required ="true" class="easyui-combobox"style="width:155" height='auto'>
                                                <option value="M">Male</option>
                                                <option value="F">Female</option>
                                            </select>
                                        </div>
                                        <div class="fitem">
                                            <label>Centre: </label>
                                            <select name="CentreName" required = "true" class="easyui-combobox"style="width:155" height='auto'>
                                                <?php
                                                include 'conn.php';
                                                if ($userType == "System Admin") {
                                                    $centreDetail = "SELECT CentreId, CentreName FROM centre";
                                                }
                                                else
                                                {
                                                    $centreDetail = "SELECT CentreId, CentreName FROM centre where CentreName='".$_SESSION['centreName']."'";
                                                }
                                                $query_result = mysql_query($centreDetail);
                                                while ($result = mysql_fetch_assoc($query_result)) {
                                                    ?>
                                                    <option value="<?php echo $result['CentreId'] ?>"><?php echo $result['CentreName'] ?></option>
                                                    <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </form>
                                </div>
                                <div id="insertdlg-buttons">
                                    <a href="#" name="save" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveClient()">Save</a>
                                    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#insertdlg').dialog('close')">Cancel</a>
                                </div>
                            </div>

                            <!-- Update Dialog and Form -->
                            <div>
                                <div id="updatedlg" class="easyui-dialog" style="width:400px;height:380px;padding:10px 20px"
                                     closed="true" buttons="#updatedlg-buttons">
                                    <div class="ftitle">Clients Information</div>
                                    <form id="updateform" method="post" novalidate>
                                        <div class="fitem">
                                            <label>NRIC: </label>
                                            <input name="NRIC" class="easyui-validatebox" required="true" readonly = "true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Name: </label>
                                            <input name="CustomerName" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Citizenship: </label>
                                            <select name="Citizenship"required ="true" class="easyui-combobox"style="width:155" height='auto'>
                                                <option value="SC">Sinagapore Citizen</option>
                                                <option value="PR">Permanent Resident</option>
                                            </select>
                                        </div>
                                        <div class="fitem">
                                            <label>Before Deviation: </label>
                                            <input name="BeforeDeviation" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>After Deviation: </label>
                                            <input name="AfterDeviation" class="easyui-validatebox" required="true"style="width:155" height='auto'>
                                        </div>
                                        <div class="fitem">
                                            <label>Date of Birth: </label>
                                            <input name="DateOfBirth" type="date" value "" data-options="formatter:myformatter,parser:myparser" class="easyui-datebox" required="true"style="width:155" height='auto' >
                                        </div>
                                        <div class="fitem">
                                            <label>Gender: </label>
                                            <select name="Gender"required ="true" class="easyui-combobox"style="width:155" height='auto'>
                                                <option value="M">Male</option>
                                                <option value="F">Female</option>
                                            </select>
                                        </div>
                                        <div class="fitem">
                                            <label>Centre: </label>
                                            <select name="CentreName" required = "true" class="easyui-combobox"style="width:155" height='auto'>
                                                <option value="-1">Withdrawn</option>
                                                <?php
                                                mysql_connect("localhost", "root", "");
                                                mysql_select_db("eldercaredb");
                                                $centreDetail = "SELECT CentreId, CentreName FROM centre";
                                                $query_result = mysql_query($centreDetail);
                                                while ($result = mysql_fetch_assoc($query_result)) {
                                                    ?>
                                                    <option value="<?php echo $result['CentreId'] ?>"><?php echo $result['CentreName'] ?></option>
                                                    <?php
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </form>
                                </div>
                                <div id="updatedlg-buttons">
                                    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateClient()">Save</a>
                                    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#updatedlg').dialog('close')">Cancel</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="clear"></div>
                    <div><p></p></div>
                </div>
            </div>
    </body> 
</html>